package battle 
{
	import Common.GameDefine;
	
	import flash.display.MovieClip;
	
	import Common.MapDirection;
	import map.MapGrid;
	import Common.GameAction;

	/**
	 * ...
	 * @author 
	 */
	public class BattleHandlerResultAction
	{
		private const TIMEHURT:Number = 200.0;
		
		private var time:Number = 0.0;
		private var hurtState:int = GameDefine.INVALIDID;
		private var hurtTime:Number = 0.0;
		
		private var attackerArray:Array = null;
		private var attackerPetArray:Array = null;
		private var targetArray:Array = null;
		private var targetPetArray:Array = null;
		private var action:BattleResultAction = null;
		
		private var attackerMcArray:Array = null;
		private var attackerPetMcArray:Array = null;
		private var targetMcArray:Array = null;
		private var targetPetMcArray:Array = null;
		
		private var moveToAttack:int = 0;
		
		private const MSINGLE:int = 0;
		private const MMULTI:int = 1;
		private const MJOINT:int = 2;
		private var mode:int = GameDefine.INVALIDID;
		
		private var state:int = 0;
		
		private var overFunction:Function = null;
		
		private var attackerActionOver:int = 0;
		private var targetActionOver:int = 0;
		private var lastAction:Boolean = false;
		
		
		public function BattleHandlerResultAction() 
		{
			
		}
		
		
		public function init( fun:Function ):void
		{
			overFunction = fun;
		}
		
		
		public function set LastAction( b:Boolean ):void
		{
			lastAction = b;
		}
		
		
		public function clear():void 
		{
			lastAction = false;
			attackerActionOver = 0;
			targetActionOver = 0;
			moveToAttack = 0;
			
			action = null;
			
			attackerArray = null;
			attackerPetArray
			targetArray = null;
			targetPetArray = null;
		}
		
		
		public function setActionCreature( a:BattleResultAction , i:int ):void
		{
			action = a;
//			index = action.owner[ i ];
//			indexPlayer = i;
			
			if ( action.attack ) 
			{
				attackerArray = BattleData.instance.playerArray;
				attackerPetArray = BattleData.instance.playerPetArray;
				
				attackerMcArray = BattleData.instance.fightMC.playerArray;
				attackerPetMcArray = BattleData.instance.fightMC.playerPetArray;

				
				targetArray = BattleData.instance.defenderArray;
				targetPetArray = BattleData.instance.defenderPetArray;
				
				targetMcArray = BattleData.instance.fightMC.enemyArray;
				targetPetMcArray = BattleData.instance.fightMC.enemyPetArray;
			}
			else
			{
				targetArray = BattleData.instance.playerArray;
				targetPetArray = BattleData.instance.playerPetArray;
				
				targetMcArray = BattleData.instance.fightMC.playerArray;
				targetPetMcArray = BattleData.instance.fightMC.playerPetArray;
				
				
				attackerArray = BattleData.instance.defenderArray;
				attackerPetArray = BattleData.instance.defenderPetArray;
				
				attackerMcArray = BattleData.instance.fightMC.enemyArray;
				attackerPetMcArray = BattleData.instance.fightMC.enemyPetArray;
			}
			
		}
		
		private function onMoveToAttack( c:BattleCreature ):void
		{
			if ( action.attack )
			{
				c.setDirection( MapDirection.NORTH );
			}
			else
			{
				c.setDirection( MapDirection.SOUTH );
			}
			
			c.setAction( GameAction.ATTACK , onMoveToAttackOver );
		}
		
		
		public function onHurtMoveOver( c:BattleCreature ):void
		{
			if ( action.resultType[0] == BattleResultCommand.BATTLEKILL )
			{
				c.data.Fall = true;
				c.setMoveSpeed( GameCreatureBattleMove.FALLSPEED );
				c.move( c.originX , c.originY , onHurtMoveFall );
				return;
			}
			
			c.setMoveSpeed( GameCreatureBattleMove.NORMALSPEED );
			c.move( c.originX , c.originY , onHurtMoveBackOver );
			
			targetActionOver--;
			checkOver();
		}
		
		
		public function onFall( c:BattleCreature ):void
		{
			c.setMoveSpeed( GameCreatureBattleMove.NORMALSPEED );
			
			if ( lastAction )
			{
				targetActionOver--;
				checkOver();
			}
		}
		
		
		public function onHurtMoveFall( c:BattleCreature ):void
		{
			c.setAction( GameAction.FALL , onFall );
			
			if ( !lastAction )
			{
				targetActionOver--;
				checkOver();
			}
		}
		
		
		public function onHurtMoveBackOver( c:BattleCreature ):void
		{
			
		}
		
		
		public function onHurtOver( c:BattleCreature ):void
		{
			
		}
		
		
		public function onMoveToAttackOver( c:BattleCreature ):void
		{
			// 要改成播放完击中动画后再撤回。
			c.move( c.originX , c.originY , onMoveBack );
			moveToAttack++;
			
			var t:Array;
			var tMc:Array;
			var index:int = c.targetIndex;
			if ( c.targetType == BattleCommand.UNDER )
			{
				t = targetArray;
				tMc = targetMcArray;
			}
			else
			{
				t = targetPetArray;
				tMc = targetPetMcArray;
			}
			
			tMc[ index ].Hit();
			
			if ( moveToAttack == action.attackerNum )
			{
				var fightMC:MovieClip = BattleData.instance.fightMC;
				
				// jump
				tMc[ index ].Jump( action.result[0] );
				
				t[ index ].data.HP -= action.result[0];
				
				if ( t[ index ].data.HP <= 0 )
				{
					t[ index ].data.HP = 1;
				}
				if ( action.resultType[0] == BattleResultCommand.BATTLEKILL )
				{
					t[ index ].data.Fall = true;
				}
				
				targetActionOver++;
				
				if ( t[ index ].action == BattleCommand.DEFENCE )
				{
					if ( action.attack ) 
					{
						t[ index ].move( c.targetX - MapGrid.gridXHalf , c.targetY - MapGrid.gridYHalf , onHurtMoveOver , false );
						t[ index ].setDirection( MapDirection.SOUTH );
					}
					else
					{
						t[ index ].move( c.targetX + MapGrid.gridXHalf , c.targetY + MapGrid.gridYHalf , onHurtMoveOver , false );
						t[ index ].setDirection( MapDirection.NORTH );
					}
					
					t[ index ].setMoveSpeed( GameCreatureBattleMove.HURTLSPEED );
					t[ index ].setAction( GameAction.DEFENCE , onHurtOver );
				}
				else
				{
					if ( action.attack )
					{
						t[ index ].move( c.targetX - MapGrid.gridXHalf , c.targetY - MapGrid.gridYHalf , onHurtMoveOver , false );
						t[ index ].setDirection( MapDirection.SOUTH );
					}
					else
					{
						t[ index ].move( c.targetX + MapGrid.gridXHalf , c.targetY + MapGrid.gridYHalf , onHurtMoveOver , false );
						t[ index ].setDirection( MapDirection.NORTH );
					}
					
					t[ index ].setMoveSpeed( GameCreatureBattleMove.HURTLSPEED );
					t[ index ].setAction( GameAction.HURT , onHurtOver );
				}
			}
			else
			{
				if ( t[ index ].getAction() == GameAction.HURT )
					t[ index ].setAction( GameAction.STAND , onHurtOver );
				else
					t[ index ].setAction( GameAction.HURT , onHurtOver );
				
				//t[ index ].updateAction();
			}
		}
		
		
		public function onMoveBack( c:BattleCreature ):void
		{
			if ( action.attack ) 
			{
				c.setDirection( MapDirection.NORTH );
			}
			else
			{
				c.setDirection( MapDirection.SOUTH );
			}
			
			c.setAction( GameAction.STAND , null );
			
			attackerActionOver--;
			
			checkOver();
		}
		
		public function checkOver():void
		{
			trace( attackerActionOver + "|" + targetActionOver );
			
			if ( !attackerActionOver && !targetActionOver )
			{
				if ( overFunction != null )
				{
					overFunction();
					overFunction = null;
					
					clear();
				}
			}
		}
		
		public function setState( s:int ):void
		{
			state = s;
			
			switch ( state )
			{
				case BattleCommand.ATTACK:
					{
						moveToAttack = 0;
						
						if ( mode == MSINGLE || mode == MJOINT )
						{
							var type0:int = action.defenderType[0];
							var target0:int = action.defender[0];
							
							for ( var i:int = 0 ; i < action.attackerNum ; i++ )
							{
								var a0:Array;
								var t0:Array;
								if ( action.attackerType[i] == BattleCommand.UNDER )
								{
									a0 = attackerArray;
								}
								else
								{
									a0 = attackerPetArray;
								}
								
								if ( type0 == BattleCommand.UNDER )
								{
									t0 = targetArray;
								}
								else
								{
									t0 = targetPetArray;
								}
								
								a0[ action.attacker[i] ].targetType = type0;
								a0[ action.attacker[i] ].targetIndex = target0;
								a0[ action.attacker[i] ].targetX = t0[ target0 ].originX;
								a0[ action.attacker[i] ].targetY = t0[ target0 ].originY;
								
								attackerActionOver++;
								
								
								if ( action.attack )
								{
									a0[ action.attacker[i] ].move( t0[ target0 ].originX + MapGrid.gridXHalf , t0[ target0 ].originY + MapGrid.gridYHalf , onMoveToAttack );
								}
								else
								{
									a0[ action.attacker[i] ].move( t0[ target0 ].originX - MapGrid.gridXHalf , t0[ target0 ].originY - MapGrid.gridYHalf , onMoveToAttack );
								}
							}
							
						}
					}
				break;
				case BattleCommand.MAGIC:
				{
					
				}
				break;
				case BattleCommand.DEFENCE:
				{
					overFunction( true );
					overFunction = null;
					
					clear();
				}
				break;
				case BattleCommand.ESCAPE:
				{
					// 怪物逃跑有问题。
					if ( attackerArray[0].data.ID == GameManager.instance.MainPlayer.ID )
					{
						overFunction( true );
						overFunction = null;
						
						clear();
					}
					else
					{
						overFunction( true );
						overFunction = null;
						
						clear();
					}
				}
				break;
				default:
				
				break;
			}
		}
		
		public function active():void 
		{
			attackerActionOver = 0;
			targetActionOver = 0;
			
			if ( action.attackerNum > 1 )
			{
				mode = MJOINT;
			}
			else if ( action.defenderNum > 1 )
			{
				mode = MMULTI;
			}
			else
			{
				mode = MSINGLE;
			}
			
			setState( action.type );	
		}
		
	}

}